package com.befirefly.studentmanager.util;

import java.sql.*;

/**
 * @Author: 八千里路云和月
 * @Date: 2021/2/7 22:28
 * @Description:
 */
public class DBUtil {
    private static final String URL = "jdbc:h2:~/test";
    private static final String DRIVER = "org.h2.Driver";
    private static final String USER_NAME ="root";
    private static final String PWD = "root";

    static {
        try {
            // 静态代码块
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    // 获取数据库连接
    public static Connection getConn() {
        try {
            return DriverManager.getConnection(URL, USER_NAME, PWD);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public static void closeConn(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public static void closePs(PreparedStatement ps) {
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public static void closeRs(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
